<?php
namespace Src;

class Model
{
    public $pdo;//从bean容器中取出mysql对象
    public $redis;//从bean容器中取出redis对象

    public function __construct()
    {
        $this->pdo = Bean::get('pdo');
        $this->redis = Bean::get('redis');
    }

    /**
     * api接口分页
     * @param $page 第几页的数据
     * @param $item 每页多少条数据
     * @return array|false
     */
    public function pagingPage($item, $page, $sql, $sqlCount)
    {
        $total = $this->pdo->select($sqlCount);
        //如果总数小于等于0的话
        if ($total[0]['count'] <= 0) {
            return false;
        }
        $total = intval($total[0]['count']);//求出总数
        $total_page = $total/$item;//求出总页数
        //如果不是整数，则进行过滤整数
        if (!is_int($total_page)) {
            $total_page = intval($total_page) + 1;//求出总页数
        }
        $page_start = $page * $item;
        $sql2_full = $sql." limit {$page_start},{$item}";
        $data = $this->pdo->select($sql2_full);
        return ['total_page' => $total_page, 'data' => $data];
    }

    /**
     * @param $sql_count
     * @param $sql
     * @param $item 每页多少条数据
     * @return array|false  非接口分页
     */
    public function pageSelf($sql_count, $sql, $item)
    {
        $pageHtml = $this->pageHtml($sql_count, $item);
        $pageData = $this->pageData($sql, $item);
        if ($pageData == false or $pageHtml == false) {
            return false;
        } else {
            return ['pageData' => $pageData, 'pageHtml' => $pageHtml];
        }
    }

    /**
     * @param $sql_count sql count 语句
     * @param $item 每页多少条数据
     * @return false|string 分页的html
     */
    public function pageHtml($sql_count, $item)
    {
        $total = $this->pdo->select($sql_count);
        //如果总数小于等于0的话
        if ($total[0]['count'] <= 0) {
            return false;
        }
        $total = intval($total[0]['count']);
        $total_page = $total/$item;
        //如果不是整数，则进行过滤整数
        if (!is_int($total_page)) {
            $total_page = intval($total_page) + 1;
        }
        //以上求出total_page的总数
        $paging = $this->bootstarp3Html($total_page);
        return $paging;
    }

    /**
     * @param $item 每页多少条数据
     * @param $sql 实际的求数据的sql
     * @return false
     */
    public function pageData($sql, $item)
    {
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $page = $page - 1;//根据url的page，显示第几页
        if ($page < 0) {
            return false;
        }
        $page_start = $page * $item;
        $sql2_full = $sql." limit {$page_start},{$item}";
        $data = $this->pdo->select($sql2_full);
        if (!empty($data)) {
            return $data;
        } else {
            return false;
        }
    }

    /**
     * @param $total_page 总页数
     * @return string
     */
    public function bootstarp3Html($total_page)
    {
        //获取当前url
        $url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
        //判断要在url后面加的？还是&
        if(strpos($url, '?')){
            $connector = '&page=';
        }else{
            $connector = '?page=';
        }
        //首页
        $url_head_link = isset($_GET['page']) && intval($_GET['page']) ? preg_replace("/page=\d*/",'page=1',$url)  : $url.$connector.'1';
        $url_head = "<nav aria-label='Page navigation' style='text-align: center;'>
		  <ul class='pagination'>
			<li>
			  <a href='{$url_head_link}' aria-label='Previous'>
				<span aria-hidden='true'>&laquo;</span>
			  </a>
			</li>";
        //中间所有页
        $url_mid = '';
        for ($i=1; $i<=$total_page; $i++) {
            $url_mid_link = isset($_GET['page']) && intval($_GET['page']) ? preg_replace("/page=\d*/",'page='.$i,$url)  : $url.$connector.$i;
            $url_mid .= "<li><a href='{$url_mid_link}'>{$i}</a></li>";
        }
        //尾页
        $url_footer_link = isset($_GET['page']) && intval($_GET['page']) ? preg_replace("/page=\d*/",'page='.$total_page,$url)  : $url.$connector.$total_page;
        $url_footer = "<li>
			  <a href='{$url_footer_link}' aria-label='Next'>
				<span aria-hidden='true'>&raquo;</span>
			  </a>
			</li>
		  </ul>
		</nav>";
        $url_foo = 	$url_head.$url_mid.$url_footer;
        return $url_foo;
    }



    private function __clone() {}

    public function __destruct() {}

}